package pers.lan.linkedlist;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/8 10:55 AM
 */
public class LC_21 {

    public static void main(String[] args) {
        System.out.println(
                new LC_21().mergeTwoLists(
                        ListNode.build(new int[]{1, 2, 4}),
                        ListNode.build(new int[]{1, 3, 4})
                )
        );

    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode h = new ListNode(0);
        ListNode p = h;
        while (l1 != null && l2 != null) {
            if (l1.val > l2.val) {
                p.next = l2;
                l2 = l2.next;
            } else {
                p.next = l1;
                l1 = l1.next;
            }
            p = p.next;
        }
        p.next = l1 == null ? l2 : l1;
        return h.next;
    }
}
